package com.chb.mains;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.chb.dto.AppResultDTO;
import com.chb.entity.Apple;
import com.chb.entity.FileDto;
import com.chb.entity.Pear;
import com.chb.entity.UntreatedFiles;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: CHB
 * @create: 2021-12-07 16:59
 **/
public class GenericMain {


    public static <T extends Comparable<? super T>> T getMax(Collection<? extends T> obj){
        if (obj.isEmpty())
            return null;

        Iterator<? extends T> iter = obj.iterator();
        T max = iter.next();

        while (iter.hasNext()) {
            T temp = iter.next();
            if (temp.compareTo(max) > 0) {
                max = temp;
            }
        }

        return max;
    }

    public static <T> T getMax(Class<T> obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println(obj);
        Constructor<T> declaredConstructor = obj.getDeclaredConstructor();
        T t = declaredConstructor.newInstance();
        return obj.getDeclaredConstructor().newInstance();

    }

    /**
     *
     * @param dbFiles 数据库中的附件
     * @param uploadFiles 前端上传的文件
     * @param <T> 要转变成的附件实体
     * @return 删除的 增加的 保持不变的 是否全部删除
     */
    public static <T> UntreatedFiles<T> processFile(List<FileDto> dbFiles, List<FileDto> uploadFiles){

        UntreatedFiles<T> untreatedFiles = new UntreatedFiles<>();
        // 带id的附件（保持不变）
        List<Long> standbys = uploadFiles.stream()
                .map(FileDto::getId).filter(Objects::nonNull).collect(Collectors.toList());
        // 不带id的附件（新增）
        List<FileDto> insert = uploadFiles.stream()
                .filter(fileDto -> fileDto.getId() == null).collect(Collectors.toList());
        // 若数据库中存在的附件，在上传的附件中不存在，则为要删除的
        if(CollectionUtils.isNotEmpty(standbys)){
            List<FileDto> toDelete = dbFiles.stream()
                    .filter(dbFile -> !standbys.contains(dbFile.getId())).collect(Collectors.toList());
            // 设置为待删除
            untreatedFiles.setToDelete((List<T>) toDelete);
        }else {
            // 上传的附件全部不带id 则删除全部图片
            untreatedFiles.setHasDeleteAll(true);
        }
//        untreatedFiles.setToInsert(insert);

        return untreatedFiles;
    }
    public static void main(String[] args) throws Exception{
        AtomicInteger atomicInteger = new AtomicInteger();
        new Thread(()->{
            ThreadLocal<Apple> apple = ThreadLocal.withInitial(() -> new Apple(2));
            ThreadLocal<Pear> pear = ThreadLocal.withInitial(() -> new Pear(4));

            System.out.println("apple 1:"+apple.get().getWeight());
            System.out.println("pear 1:"+pear.get().getWeight());

        }).start();

        new Thread(()-> {
            ThreadLocal<Apple> apple = ThreadLocal.withInitial(() -> new Apple(23));
            ThreadLocal<Pear> pear = ThreadLocal.withInitial(() -> new Pear(43));

            System.out.println("apple 2:"+apple.get().getWeight());
            System.out.println("pear 2:"+pear.get().getWeight());
        }).start();
    }
    public AppResultDTO<Integer> findByConfigKey() {

        return new AppResultDTO<String>().data("");
    }

}
